import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

class Student{

}
class IntCmp implements Comparator<Integer> {

    //写成o1.compareTo(o2) 就是小根堆;
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}
public class Main {
    //前k个最大元素 时间复杂度O(n*logk)
    public static int[] maxLestK(int[] array, int k) {
        int[] ret = new int[k];
        if(array == null || k <= 0){
            return ret;
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        //先建立一个大小为 k的小根堆
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(array[i]);
        }
        //遍历剩下的元素
        for (int i = k; i < array.length; i++) {
             int tmp = priorityQueue.peek();
             //如果比堆顶元素大，就把堆顶元素删除 并且把当前元素加入堆
            if(array[i] > tmp){
                priorityQueue.poll();
                priorityQueue.offer(array[i]);
            }
        }
        //堆中剩余的元素就是最大的k个元素
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
    }



    //top k问题的简单解法(前k个最小元素)，不是最好的
    public   int[] smallestK(int[] array, int k) {
        int[] ret = new int[k];
        if(array == null || k<0){
            return ret;
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(array.length);
        for (int i = 0; i < array.length; i++) {
            priorityQueue.offer(array[i]);
        }
        for (int j = 0; j < k; j++) {
            priorityQueue.poll();
        }
        return ret;
    }


    //前k个最小元素 时间复杂度O(n*logk)
    public static int[] smallestK2(int[] array, int k) {
        int[] ret = new int[k];
        if(array == null || k <= 0){
            return ret;
        }

        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new IntCmp());
        //先建立一个大小为 k的大根堆
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(array[i]);
        }
        //遍历剩下的元素
        for (int i = k; i < array.length; i++) {
            int tmp = priorityQueue.peek();
            //如果比堆顶元素小，就把堆顶元素删除 并且把当前元素加入堆
            if(array[i] < tmp){
                priorityQueue.poll();
                priorityQueue.offer(array[i]);
            }
        }
        //堆中剩余的元素就是最大的k个元素
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
    }

    public static void main(String[] args) {
        //默认情况下是一个小根堆
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        /*priorityQueue.offer(10);
        priorityQueue.offer(5);
        priorityQueue.offer(9);
        priorityQueue.offer(7);
        priorityQueue.offer(77);
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());

        PriorityQueue<Student> priorityQueue1 = new PriorityQueue<>();
        //priorityQueue1.offer(null); 不能传null*/

        int[] array = {27,15,19,18,28,34,65,49,25,37};


         int[] ret = maxLestK(array,5);
        System.out.println(Arrays.toString(ret));


        int[] s = smallestK2(array,3);
        System.out.println(Arrays.toString(s));
    }
    public static void main1(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        testHeap.initHeap(array);

        testHeap.CreateHeap();

        System.out.println("===插入元素==");
        testHeap.offer(80);




        System.out.println("===删除元素==");
        System.out.println(testHeap.poll());

    }
}